package com.ayoka.parenting.ui.login;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.ViewGroup;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.NVCBean;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.ayoka.parenting.widget.ProgressWebView;
import com.jincp.common.dialog.DialogDismissListener;
import com.jincp.common.utils.UIUtils;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: LoginPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter{

    private LoginModel model;

     public LoginPresenter() {
        model = new LoginModel();
     }


    public void sendVerifyCode(String token, String mobile) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.sendVerifyCode(this, token, mobile, new SimpleRequestCallback<BaseResponse<NVCBean>>() {
            @Override
            public void onSuccess(BaseResponse<NVCBean> data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onSendVerifyCodeCallback(data.data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onSendVerifyCodeCallback(null);
            }
        });
    }

    public void login(String token, String mobile, String verifyCode) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.login(this, token, mobile, verifyCode, new SimpleRequestCallback<BaseResponse<NVCBean>>() {
            @Override
            public void onSuccess(BaseResponse<NVCBean> data) {
                if (!isAttached()) return;
                UserManager.setUserInformation(data.data);
                mView.get().hideWaitDialog();
                mView.get().onLoginCallback(data.data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onLoginCallback(null);
            }
        });
    }

    public void showWebViewVerifyDialog(FragmentManager fragmentManager, int bizCode, ProgressWebView progressWebview) {
        WebViewVerifyDialog dialog = new WebViewVerifyDialog.Builder()
                .setGravity(Gravity.CENTER)
                .setSize(UIUtils.dp2px(300), ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimation(R.style.ScaleAnimation)
                .build();
        dialog.setCode(bizCode);
        dialog.setWebView(progressWebview);
        dialog.setDialogDismissListener(new DialogDismissListener() {
            @Override
            public void dismiss(DialogFragment dialog) {
                if (!isAttached())return;
                mView.get().onDialogDismiss(dialog);
            }
        });
        dialog.show(fragmentManager, "WebViewVerifyDialog");
    }
}
